package com.runen.wnhz.runen.presenter.iPresenter;

import android.util.Log;

import com.runen.wnhz.runen.common.rx.RxHttpReponseCompat;
import com.runen.wnhz.runen.common.rx.subscriber.ErrorHandlerSubscriber;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.BaseEntity;
import com.runen.wnhz.runen.data.entity.LessonListEntity;
import com.runen.wnhz.runen.data.entity.LessonTypeBean;
import com.runen.wnhz.runen.presenter.BasePresenter;
import com.runen.wnhz.runen.presenter.Contart.MajorContart;
import com.runen.wnhz.runen.presenter.model.MajorModel;
import com.runen.wnhz.runen.ui.fragment.major.MajorFragment;
import com.runen.wnhz.runen.ui.fragment.minem.HomeFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018-05-04.
 * MVP P
 */

public class IMajorPersenter extends BasePresenter<MajorModel, MajorContart.View> {

    //直接注入进入了
    @Inject
    public IMajorPersenter(MajorModel mModel, MajorContart.View mView) {
        super(mModel, mView);
    }

    public void getLessonType(Map<String, String> map) {
        mModel.getLessonType(map).compose(RxHttpReponseCompat.<List<LessonTypeBean>>compatResult())
                .subscribe(new Action1<List<LessonTypeBean>>() {
                    @Override
                    public void call(List<LessonTypeBean> lessonTypeBeans) {
                        mView.getLessonTypeRequest(lessonTypeBeans);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.e(MajorFragment.TAG, throwable.getMessage());
                    }
                });
    }

    public void getLessonList(int type, int page, int size, final int form_id) {

        Log.e("---------", "getLessonList");

        Map<String, Integer> map = new HashMap<>();
        map.put("type", type);
        map.put("page", page);
        map.put("size", size);
        map.put("form_id", form_id);

        mModel.getLessonList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity<LessonListEntity>>() {
                    @Override
                    public void call(BaseEntity<LessonListEntity> lessonListEntityBaseEntity) {
                        switch (lessonListEntityBaseEntity.getRe()) {
                            case 0:
                                ToastUtil.showToast("没有数据");
                                break;
                            case 1:
                                switch (form_id) {
                                    case 1:
                                        mView.getProfessionalRequest(lessonListEntityBaseEntity.getData());
                                        break;
                                    case 2:
                                        mView.getPublicRequest(lessonListEntityBaseEntity.getData());
                                        break;
                                    case 3:
                                        mView.getProjectRequest(lessonListEntityBaseEntity.getData());
                                        break;
                                }
                                break;
                            case -1:
                                break;
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.e(MajorFragment.TAG, throwable.getMessage());
                    }
                });
    }
}
